Questions
10 of 24
1How does NestJS resolve constructor injection at runtime?
2What does @Optional() decorator do in NestJS?
3What is the difference between moduleRef.get() and moduleRef.resolve() in NestJS?
4When do you need @Inject(TOKEN) vs plain constructor injection in NestJS?
5What is ModuleRef in NestJS and when would you use it?
6What are the three provider scopes in NestJS and when is each appropriate?
7What is the correct architectural solution for circular dependencies beyond forwardRef()?
8How do you detect and debug circular dependency errors in large NestJS projects?
9What is property-based injection in NestJS and when should you use it?
10How do you write a unit test for a NestJS service that has injected dependencies?
11What is a DI token and what types can be used as tokens in NestJS?
12How do you pass a custom REQUEST object to a REQUEST-scoped provider for queues or CRON jobs in NestJS?
13How does forwardRef() solve circular dependencies in NestJS and how does it work internally?
14How do you implement the Strategy pattern using NestJS DI?
15How do you replace a provider in a NestJS test module using overrideProvider()?
16What is Inversion of Control (IoC) and how does NestJS implement it?
17What is the difference between Dependency Injection (DI) and Inversion of Control (IoC)?
18What is scope propagation in NestJS and why can it impact performance?
19How do you inject the raw HTTP request object inside a REQUEST-scoped provider in NestJS?
20How does moduleRef.resolve() work with scoped providers and what is ContextIdFactory used for?
21What role does reflect-metadata play in NestJS DI?
22What is a circular dependency in NestJS and how does it manifest at runtime?
23What is LazyModuleLoader in NestJS and how does it differ from standard DI?
24What happens to REQUEST-scoped providers during WebSocket connections or microservice message handling in NestJS?
10 / 24

How do you write a unit test for a NestJS service that has injected dependencies?

Use Test.createTestingModule() to create an isolated module, replace real providers with jest mock objects using useValue, compile the module, then retrieve the service under test with module.get(). This allows testing service logic without needing a real database, HTTP client, or other infrastructure.

Unit test with mocked dependencies
Key testing patterns:
  1. 1

    Use useValue with jest.fn() mocks — avoids spinning up real infrastructure.

  2. 2

    module.get(Token) retrieves the instantiated provider from the test container.

  3. 3

    Assign the mock to a variable so you can configure return values per test with mockResolvedValue or mockReturnValue.

  4. 4

    For providers from imported modules use .overrideProvider(Token).useValue(mock) instead of redeclaring them.

Difficulty: 5/10
Topics: Dependency Injection, Jest mocking, Nest TestingModule

Scenario Questions

0-2 years experience
  1. 1

    You have a simple NestJS service that depends on a ConfigService. Walk me through how you would write a unit test for a method that reads a config value.

  2. 2

    If you forget to provide a mock for an injected dependency, what error will you see when the test runs, and how do you fix it?

2-5 years experience
  1. 1

    Imagine you need to add a new method to an existing service that calls an external API client injected via the constructor. How would you structure the unit test to isolate the service logic and simulate different API responses?

  2. 2

    During a code review you notice the test suite is flaky because the mock for a database repository returns undefined on some runs. How would you debug and stabilize the mock implementation?

5-8 years experience
  1. 1

    You are leading a team that is refactoring several services to share a common logging provider. What testing strategy would you adopt to ensure each service's unit tests remain fast and reliable while the shared provider is mocked?

  2. 2

    When scaling the codebase, you notice the number of mock files is growing unmanageable. How would you redesign the testing approach for injected dependencies to reduce duplication and improve maintainability?

8+ years experience
  1. 1

    At the architecture level, how would you design a testing framework for a large NestJS monorepo so that unit tests for services with deep dependency trees stay isolated, and what trade‑offs does your approach have?

  2. 2

    If the organization decides to migrate from Jest to a different test runner, what impact does that have on the way you mock injected providers, and how would you plan the migration to minimize risk?

Follow-up Questions

  • How would you verify that a mocked repository method was called with the correct arguments?
  • What would you do if the dependency you need to mock is itself a NestJS module with its own providers?
  • Can you explain why you might choose a manual mock over jest.spyOn in this context?